/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.umum.msi.listener;

import java.util.ArrayList;
import java.util.List;

import lioncen.cti.jmsi.engine.MSIEventListener;
import cn.umum.msi.User;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;

/**
 *
 * @author Administrator
 */
public class MSIAdapter implements MSIEventListener {

    private static int count = 0;
    private static int msiStartsuccessed = 0;
    int AgentID;
     public MSIAdapter(int agentid) {
                AgentID = agentid;
                }

    public void socketConnected(int connid) {
        count = connid;
        msiStartsuccessed = 1;
        System.out.println("----socket Connected----");
        System.out.println(" connid=" + connid);
    }

    public void socketClosed(int connid) {
        msiStartsuccessed = 0;
        count = -1;
        System.out.println("----socket Closed----");
        System.out.println(" connid=" + connid);
        agentStats.remove("A"+AgentID);
        //agentStats.clear();
    }

    public void callIncome(String alertingDN, String callingDN, String calledDN, String UUD) {
        count = -2;
        System.out.println("----Call Income----");
        System.out.println("alertingDN:" + alertingDN);
        System.out.println("callingDN:" + callingDN);
        System.out.println("calledDN:" + calledDN);
        System.out.println("UUD:" + UUD);
        //System.out.println(findUserid(calledDN));
        User u = findUser(agentStats, "A"+AgentID);
        if (u != null) {
            //agentStats.put(u.getUserid(), u);
            user = new User();
            user.setUserid(u.getUserid());
            user.setUsername(u.getUsername());
            user.setagentDN(u.getagentDN());
            user.setUserStat(4);
            user.setCallingDN(callingDN);
            user.setCalledDN(calledDN);
            //agentStatsChange.put(u.getUserid(), user);
            agentStats.put(u.getUserid(), user);
            
        }
    //showMap(agentStats);
    //user = new User("uid"+alertingDN, "username"+callingDN);
    //agentStatsChange.add(user);
    }

    public void fskData(String dn, String fskData, int fskFormat, int taskid) {
        count = -3;
        System.out.println("----fsk Data---");
        System.out.println(" dn=" + dn);
        System.out.println(" fskData=" + fskData);
        System.out.println(" fskFormat=" + fskFormat);
        System.out.println(" taskid=" + taskid);
    }

    public void hookStatusChanged(String selfDN, int hookStatus) {
        count = -4;
        System.out.println("----hook Status Changed---");
        System.out.println(" selfDN=" + selfDN);
        System.out.println(" hookStatus=" + hookStatus);
        int userstats=2;
        if(hookStatus==2){
            userstats=6;
        }else if(hookStatus==3){
            userstats=7;
        }else if(hookStatus==4){
            userstats=8;
        }
        User u = findUser(agentStats, "A"+AgentID);
        if (u != null) {
            user = new User();
            user.setUserid(u.getUserid());
            user.setUsername(u.getUsername());
            user.setagentDN(u.getagentDN());
            user.setUserStat(userstats);
            user.setCalledDN(selfDN);
            //agentStatsChange.put(u.getUserid(), user);
            agentStats.put(u.getUserid(), user);
        }
    //Todo 话柄状态更改对应的操作

    }

    public void answered(String dn, String callingDN, String calledDN, String UUD) {
        count = -5;
        System.out.println("----agent answered---");
        System.out.println(" dn=" + dn);
        System.out.println(" callingDN=" + callingDN);
        System.out.println(" UUD=" + UUD);
        User u = findUser(agentStats, "A"+AgentID);
        if (u != null) {
            user = new User();
            user.setUserid(u.getUserid());
            user.setUsername(u.getUsername());
            user.setagentDN(u.getagentDN());
            user.setUserStat(5);
            user.setCallingDN(callingDN);
            user.setCalledDN(dn);
            //agentStatsChange.put(u.getUserid(), user);
            agentStats.put(u.getUserid(), user);
        }
    }

    public void agentStatusChanged(int agentID, int agentStatus) {
        count = -6;
        System.out.println("----agent Status Changed---");
        System.out.println(" agentID=" + agentID);
        //user = new User("uid"+agentID, "username"+agentID);
        //users.add(user);
        System.out.println(" newAgtStatus=" + agentStatus);
        System.out.println(" AgentID=" + AgentID);
    }

    public void asyncFinished(String dn, int optype, int taskid, int ret) {
        count = -7;
        System.out.println("----async Finished---");
        System.out.println(" dn=" + dn);
        System.out.println(" optype=" + optype);
        System.out.println(" taskid=" + taskid);
        System.out.println(" ret=" + ret);
    }

    public void ringingStopped(String selfDN) {
        count = -8;
        System.out.println("----ringing Stopped---");
        System.out.println(" selfDN=" + selfDN);
        User u = findUser(agentStats, "A"+AgentID);
        if (u != null) {
            user = new User();
            user.setUserid(u.getUserid());
            user.setUsername(u.getUsername());
            user.setagentDN(u.getagentDN());
            user.setUserStat(2);
            user.setCalledDN(selfDN);
            //agentStatsChange.put(u.getUserid(), user);
            agentStats.put(u.getUserid(), user);
        }
    // put(u.getUserid(), user);
    }

    public void receiveTextMessage(String selfDN, int fromAgentID, int chatMode, String text) {
        count = -9;
        System.out.println("----receive Text Message---");
        System.out.println(" selfDN=" + selfDN);
        System.out.println(" fromAgentID=" + fromAgentID);
        System.out.println(" chatMode=" + chatMode);
        System.out.println(" text=" + text);
    }

    public void acdAgentAllBusy(String dn, String acdDN, boolean isAllBusy, int acdQueLength) {
        count = -10;
        System.out.println("----acd Agent All Busy---");
        System.out.println(" dn=" + dn);
        System.out.println(" acdDN=" + acdDN);
        System.out.println(" isAllBusy=" + isAllBusy);
        System.out.println(" acdQueLength=" + acdQueLength);
    }

    public void talkInform(String alertingDN, String callingDN, String calledDN, String UUD) {
        count = -11;
        System.out.println("----talk In form---");
        System.out.println(" alertingDN=" + alertingDN);
        System.out.println(" callingDN=" + callingDN);
        System.out.println(" callingDN=" + callingDN);
        System.out.println(" UUD=" + UUD);
    }

    public void dtmfInform(String dn, String dtmf) {
        count = -12;
        System.out.println("----dtmf In form---");
        System.out.println(" dn=" + dn);
        System.out.println(" dtmf=" + dtmf);
    }

    public void dialoutDTMFInform(String dn, String calledDN) {
        count = -13;
        System.out.println("----dialoutDTMF In form---");
        System.out.println(" dn=" + dn);
        System.out.println(" calledDN=" + calledDN);
    }

    public void ctiMasterChanged(String masterIPAddr, int masterPort, String slaveIPAddr, int slavePort) {
        count = -14;
        System.out.println("----cti Master Changed---");
        System.out.println(" masterIPAddr=" + masterIPAddr);
        System.out.println(" masterPort=" + masterPort);
        System.out.println(" slaveIPAddr=" + slaveIPAddr);
        System.out.println(" slavePort=" + slavePort);
    }

    public static int getCount() {
        return (count);
    }

    public static List getUsers() {
        return (users);
    }

    public User findUser(Map map, String userID) {
        try {

            // We could optimize this, but since there are less than 20 people
            // in the queue ...

            for (Object o : map.keySet()) {
                User a = (User) map.get(o);
                if (a.getUserid().equals(userID) == true) {
                    return a;
                }

            }

            return null;
        } catch (NumberFormatException ex) {
            log.warn("Illegal number format: " + userID, ex);
            return null;
        }
    }
    public static List<User> users = new ArrayList<User>();
    public User user = null;
    //CallimcomeUser user=null;
    public static Map<String, User> agentStatsChange = new HashMap<String, User>();
    public static Map<String, User> agentStats = new HashMap<String, User>();
    private static final Log log = LogFactory.getLog(MSIAdapter.class);
}
